//
//  DKBaseInfoAuthCell.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/5.
//

#import "DKBaseInfoAuthCell.h"
#import "DKBaseInfoSelectView.h"
#import "DKBaseInfoInputView.h"

static NSString * DKBaseInfoAuthCellId = @"DKBaseInfoAuthCellId";
@interface DKBaseInfoAuthCell()
@property (nonatomic, strong)UIView * dk_BaseInfoBgView;
@property (nonatomic, strong)DKBaseInfoSelectView * dk_EducationLevelSelectView;
@property (nonatomic, strong)DKBaseInfoInputView * dk_MonthlySalaryInputView;
@property (nonatomic, strong)DKBaseInfoSelectView * dk_ResidenceSelectView;
@property (nonatomic, strong)DKBaseInfoSelectView * dk_ProfessionSelectView;
@property (nonatomic, strong)DKBaseInfoSelectView * dk_MaritalStatusSelectView;
@property (nonatomic, strong)DKBaseInfoInputView * dk_EmailAddressInputView;
@property (nonatomic, strong)UILabel * dk_EmailAddressInputErrorAlertLab;
@property (nonatomic, strong)UIButton * dk_NextBtn;
@end
@implementation DKBaseInfoAuthCell
+ (instancetype)baseInfoAuthCell:(UITableView *)tableView{
    DKBaseInfoAuthCell * dk_BaseInfoAuthCell = [tableView dequeueReusableCellWithIdentifier:DKBaseInfoAuthCellId];
    if (!dk_BaseInfoAuthCell) {
        dk_BaseInfoAuthCell = [[DKBaseInfoAuthCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DKBaseInfoAuthCellId];
    }
    dk_BaseInfoAuthCell.backgroundColor = [UIColor clearColor];
    dk_BaseInfoAuthCell.selectionStyle = UITableViewCellSelectionStyleNone;
    dk_BaseInfoAuthCell.accessoryType = UITableViewCellAccessoryNone;
    return dk_BaseInfoAuthCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    WeakSelf(self)
    self.dk_BaseInfoBgView = [[UIView alloc] init];
    self.dk_BaseInfoBgView.backgroundColor = [UIColor whiteColor];
    self.dk_BaseInfoBgView.layer.cornerRadius = DK_IsWidthScale(50);
    [self.contentView addSubview:self.dk_BaseInfoBgView];
    [self.dk_BaseInfoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(30)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    UIView * dk_PanCardBottomView = [[UIView alloc] init];
    dk_PanCardBottomView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:dk_PanCardBottomView];
    [dk_PanCardBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    //
    self.dk_EducationLevelSelectView = [[DKBaseInfoSelectView alloc] init];
    self.dk_EducationLevelSelectView.authBaseStepSelectType = AuthBaseStepSelectTypeEducationLevel;
    [self.dk_BaseInfoBgView addSubview:self.dk_EducationLevelSelectView];
    [self.dk_EducationLevelSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_EducationLevelSelectView.SelectScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_MonthlySalaryInputView = [[DKBaseInfoInputView alloc] init];
    self.dk_MonthlySalaryInputView.authBaseStepInputType = AuthBaseStepInputTypeMonthlySalary;
    [self.dk_BaseInfoBgView addSubview:self.dk_MonthlySalaryInputView];
    [self.dk_MonthlySalaryInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_EducationLevelSelectView.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_MonthlySalaryInputView.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_ResidenceSelectView = [[DKBaseInfoSelectView alloc] init];
    self.dk_ResidenceSelectView.authBaseStepSelectType = AuthBaseStepSelectTypeResidence;
    [self.dk_BaseInfoBgView addSubview:self.dk_ResidenceSelectView];
    [self.dk_ResidenceSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_MonthlySalaryInputView.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_ResidenceSelectView.SelectScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_ProfessionSelectView = [[DKBaseInfoSelectView alloc] init];
    self.dk_ProfessionSelectView.authBaseStepSelectType = AuthBaseStepSelectTypeProfession;
    [self.dk_BaseInfoBgView addSubview:self.dk_ProfessionSelectView];
    [self.dk_ProfessionSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_ResidenceSelectView.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_ProfessionSelectView.SelectScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_MaritalStatusSelectView = [[DKBaseInfoSelectView alloc] init];
    self.dk_MaritalStatusSelectView.authBaseStepSelectType = AuthBaseStepSelectTypeMaritalStatus;
    [self.dk_BaseInfoBgView addSubview:self.dk_MaritalStatusSelectView];
    [self.dk_MaritalStatusSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_ProfessionSelectView.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_MaritalStatusSelectView.SelectScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_EmailAddressInputView = [[DKBaseInfoInputView alloc] init];
    self.dk_EmailAddressInputView.authBaseStepInputType = AuthBaseStepInputTypeEmailAddress;
    [self.dk_BaseInfoBgView addSubview:self.dk_EmailAddressInputView];
    [self.dk_EmailAddressInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_MaritalStatusSelectView.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_EmailAddressInputView.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    self.dk_EmailAddressInputErrorAlertLab = [[UILabel alloc] init];
    self.dk_EmailAddressInputErrorAlertLab.textColor = DK_Hex(@"#ED4342");
    self.dk_EmailAddressInputErrorAlertLab.font = DK_FONT_Regular(10);
    self.dk_EmailAddressInputErrorAlertLab.text = @"*Please enter a valid email address";
    self.dk_EmailAddressInputErrorAlertLab.hidden = YES;
    [self addSubview:self.dk_EmailAddressInputErrorAlertLab];
    [self.dk_EmailAddressInputErrorAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(40)));
        make.top.equalTo(self.dk_EmailAddressInputView.mas_bottom).offset(DK_IsWidthScale(0));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    //next
    self.dk_NextBtn = [[UIButton alloc] init];
    [self.dk_NextBtn setTitle:@"Next" forState:UIControlStateNormal];
    [self.dk_NextBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.dk_NextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_NextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_NextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_NextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.dk_NextBtn.enabled = NO;
    [self.dk_NextBtn addTarget:self action:@selector(goCheck) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_BaseInfoBgView addSubview:self.dk_NextBtn];
    [self.dk_NextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_EmailAddressInputErrorAlertLab.mas_bottom).offset(DK_IsWidthScale(20));
    }];
    UIButton * dk_AgreementBtn = [[UIButton alloc] init];
    [dk_AgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_AgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_AgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_AgreementBtn addTarget:self action:@selector(CheckAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_BaseInfoBgView addSubview:dk_AgreementBtn];
    [dk_AgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.dk_NextBtn.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
}
//-(void)layoutSublayersOfLayer:(CALayer *)layer{
//    [super layoutSublayersOfLayer:layer];
//    [self.dk_BaseInfoBgView makeCornerOnTopleftRight:DK_IsWidthScale(50)];
//}
- (void)makeNextStatus{
    if([self.dk_EmailAddressInputView getStepStatusInput].length > 0){
        BOOL isEmailAddress = [NSString makeEmailAddress:[self.dk_EmailAddressInputView getStepStatusInput]];//验证邮箱正确否
        if(!isEmailAddress){
            [self.dk_EmailAddressInputView changeErrorStepStatusInput:YES];
            self.dk_EmailAddressInputErrorAlertLab.hidden = NO;
            [self.dk_EmailAddressInputErrorAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.dk_EmailAddressInputView.mas_bottom).offset(DK_IsWidthScale(10));
                make.height.equalTo(@(DK_IsWidthScale(14)));
            }];
        }else{
            [self.dk_EmailAddressInputView changeErrorStepStatusInput:NO];
            self.dk_EmailAddressInputErrorAlertLab.hidden = YES;
            [self.dk_EmailAddressInputErrorAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.dk_EmailAddressInputView.mas_bottom).offset(DK_IsWidthScale(0));
                make.height.equalTo(@(DK_IsWidthScale(0)));
            }];
        }
    }
    
    if(([self.dk_EducationLevelSelectView getStepStatusSelect].length > 0)&&([self.dk_MonthlySalaryInputView getStepStatusInput].length > 0)&&([self.dk_ResidenceSelectView getStepStatusSelect].length > 0)&&([self.dk_ProfessionSelectView getStepStatusSelect].length > 0)&&([self.dk_MaritalStatusSelectView getStepStatusSelect].length > 0)&&([self.dk_EmailAddressInputView getStepStatusInput].length > 0)){
        BOOL isEmailAddress = [NSString makeEmailAddress:[self.dk_EmailAddressInputView getStepStatusInput]];//验证邮箱正确否
        if(isEmailAddress){
            self.dk_NextBtn.enabled = YES;
        }else{
            self.dk_NextBtn.enabled = NO;
        }
    }else{
        self.dk_NextBtn.enabled = NO;
    }
}
- (void)goCheck{
    [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_VerifyBasic withAction:DK_ActionClick complete:^{
            
    } fail:^{
            
    }];
    
    NSMutableDictionary *userInfoDataDic = [[NSMutableDictionary alloc] init];
    [userInfoDataDic setObject:self.dk_EducationLevelSelectView.authCommonDicMdl.typeCode forKey:@"educationLevel"];
    [userInfoDataDic setObject:[self.dk_EmailAddressInputView getStepStatusInput] forKey:@"email"];
    [userInfoDataDic setObject:[self.dk_MonthlySalaryInputView getStepStatusInput] forKey:@"monthlyIncome"];
    [userInfoDataDic setObject:self.dk_ProfessionSelectView.authCommonDicMdl.typeCode forKey:@"occupation"];
    [userInfoDataDic setObject:self.dk_MaritalStatusSelectView.authCommonDicMdl.typeCode forKey:@"marriage"];
    [userInfoDataDic setObject:self.dk_ResidenceSelectView.authCommonDicMdl.typeCode forKey:@"residenceType"];
    NSMutableDictionary *userInfoDic = [[NSMutableDictionary alloc] init];
    [userInfoDic setObject:userInfoDataDic forKey:@"data"];
    [userInfoDic setObject:@"PERSONAL_INFO" forKey:@"step"];
    
    [DKPublicHttp apiSaveUserInfoForAPI:userInfoDic complete:^{//保存用户信息
        [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_VerifyBasic withAction:DK_ActionSave complete:^{
                
        } fail:^{
                
        }];

        if(self.RequestScuessBaseInfoBlock){
            self.RequestScuessBaseInfoBlock();
        }
    }fail:^{
        
    }];
}
- (void)CheckAgreement{
    if(self.CheckPrivateAgrmentBlock){
        self.CheckPrivateAgrmentBlock();
    }
}
@end
